package com.czl.module_service.fragment

import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.TimeUtils
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.event.LiveBusCenter
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.databinding.FragmentHousesPaymentDetailBinding
import com.czl.module_service.viewmodel.HousesPaymentDetailViewModel
import java.util.*

@Route(path = AppConstants.Router.Service.F_HOUSES_PAYMENT_DETAIL_FRAGMENT)
class HousesPaymentDetailFragment : BaseFragment<FragmentHousesPaymentDetailBinding, HousesPaymentDetailViewModel>() {

    override fun initContentView(): Int {
        return R.layout.fragment_houses_payment_detail
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        viewModel.tvTitle.set("缴费详情")
        viewModel.billId =
            arguments?.getSerializable("billId").toString().toInt()
        viewModel.getDetailsByBillId(billId = viewModel.billId)
    }

    override fun initViewObservable() {
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            data?.payableDateShortStr = TimeUtils.date2String(Date(data?.payableDate!!), "yyyy-MM")
            data.payableDateStr = TimeUtils.date2String(Date(data.payableDate!!), "yyyy-MM-dd")
//            data.payStatusName = if (data.payStatus == 0) "未缴" else "已缴"
            if (data.payStatus == 0 && data.payableMoney?.toDouble()!! >= 0) {
                data.isPay = 1
                data.payStatusName = "待缴"
            } else if (data.payStatus == 0 && data.payableMoney?.toDouble()!! < 0) {
                data.isPay = 2
                data.payStatusName = "待退款"
            } else if (data.payStatus == 1 && data.payableMoney?.toDouble()!! >= 0) {
                data.isPay = 3
                data.payStatusName = "已缴"
            } else if (data.payStatus == 1 && data.payableMoney?.toDouble()!! < 0) {
                data.isPay = 4
                data.payStatusName = "已退款"
            }
            data.billDateStr = TimeUtils.date2String(Date(data.billDate!!), "yyyy-MM-dd")
            data.billEndDateStr = TimeUtils.date2String(Date(data.billEndDate!!), "yyyy-MM-dd")
            viewModel.bean.set(data)
//            viewModel.setData(data)
        })
        viewModel.uc.onPayClickEvent.observe(this, {
            startContainerActivity(AppConstants.Router.Pay.F_PAY_DETAIL, Bundle().apply {
                putString(AppConstants.BundleKey.PAY_ORDER_KEY, it)
                putString(
                    AppConstants.BundleKey.HOUSE_ORDER_IS,
                    AppConstants.Constants.IS_HOUSE_ORDER
                )
            })
        })
        LiveBusCenter.observePayResultEvent(this, {
            when (it.payType) {
                AppConstants.Constants.PAY_SUCCESS_TYPE -> {
                    viewModel.getDetailsByBillId(billId = viewModel.billId)
                }
                AppConstants.Constants.PAY_FAIL_TYPE -> {
                    back()
                }
            }
        })
    }

}